package com.wgl.yygh.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wgl.model.hosp.Schedule;
import com.wgl.model.vo.msm.MsmVo;
import com.wgl.model.vo.order.OrderMqVo;
import com.wgl.rocket.service.RocketService;
import com.wgl.yygh.serivce.ScheduleService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName HospReceiver.java
 * @Description TODO
 * @createTime 2022年02月14日 15:24:00
 */
@Component
public class HospReceiver {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private RocketService rocketService;
    @Value("${rocketmq.nameserver}")
    private String nameserver;

    @PostConstruct
    public void send(){
        DefaultMQPushConsumer yygh_xf = new DefaultMQPushConsumer("yygh_hosp");
        //连接NameServer
        yygh_xf.setNamesrvAddr(nameserver);
        try {
            yygh_xf.subscribe("yygh", "yygh_orde");
            yygh_xf.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    msgs.forEach(messageExt -> {
                        System.out.println("hosp消费");

                        OrderMqVo orderMqVo =  JSONObject.parseObject(new String(messageExt.getBody()),OrderMqVo.class);
                        System.out.println(orderMqVo);
                        if(null != orderMqVo.getAvailableNumber()) {
                            //下单成功更新预约数
                            Schedule schedule = scheduleService.getScheduleId(orderMqVo.getScheduleId());
                            schedule.setReservedNumber(orderMqVo.getReservedNumber());
                            schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
                            scheduleService.updatePb(schedule);
                        } else {
                            //取消预约更新预约数
                            Schedule schedule = scheduleService.getScheduleId(orderMqVo.getScheduleId());
                            int availableNumber = schedule.getAvailableNumber().intValue() + 1;
                            schedule.setAvailableNumber(availableNumber);
                            scheduleService.updatePb(schedule);
                        }

                        //发送短信
                        MsmVo msmVo = orderMqVo.getMsmVo();
                        if(null != msmVo) {
                            rocketService.sendMessage( "fdx", msmVo);
                        }
                    });
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            yygh_xf.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        System.out.println("hosp消费者启动成功!");
    }
}
